package _map

import "sync"

type RWMap[V any] struct {
	sync.RWMutex
	m map[string]V // 由于底层map不能用泛型，暂时使用string
}

func NewRWMap[V any]() *RWMap[V] {
	return &RWMap[V]{
		m: make(map[string]V, 10),
	}
}

func (m *RWMap[V]) Get(key string) (V, bool) {
	m.RLock()
	defer m.RUnlock()
	v, existed := m.m[key]
	return v, existed
}

func (m *RWMap[V]) Set(key string, value V) {
	m.Lock()
	defer m.Unlock()
	m.m[key] = value
}

func (m *RWMap[V]) remove(key string) {
	m.Lock()
	defer m.Unlock()
	delete(m.m, key)
}
